var define = {};
define.TYPE_KAMIBUKURO = "KAMIBUKURO";
define.TYPE_TB = "TB";
define.TYPE_BARA = "BARA";
define.TYPE_OTHER = "OTHER";
define.CHECK_TYPE_WEIGHT = "TYPE_GET_WEIGHT";
define.CHECK_TYPE_QUANTITY = "TYPE_GET_QUANTITY";
define.type = "";
var message = new Message();
var Util = function() {
};
Util.prototype = {
	ajax : function(type, url, dataType, async, data) {
		return $.ajax({
			type : type,
			url : url,
			dataType : dataType,
			async : async,
			data : data
		});
	}
};
$(document)
		.ready(
				function() {
					var type = "";
					var local = window.location.pathname;
					var historyref = document.referrer;
					if (local.indexOf('search.action') > -1) {
						changeColor(".btn-register");
					} else if ((local.indexOf('history') > -1)
							|| (local.indexOf('searchDpln') > -1)) {
						changeColor(".btn-history");
					} else if (local.indexOf('yoyakulist') > -1
							|| local.indexOf('searchYoyakuList') > -1
							|| local.indexOf('edit') > -1) {
						changeColor(".btn-edit");
					} else if (local.indexOf('changepasswd') > -1) {
						changeColor(".btn-passwdchange");
					} else if (local.indexOf('booking.action') > -1) {
						if (historyref.indexOf('history') > -1)
							changeColor(".btn-history");
						else
							changeColor(".btn-register");
					}

					/**
					 * set color of line that has [レビュー]button → highlight
					 * 
					 */
					$(".btn-review").closest("tr").addClass("tr-review");
					/**
					 * add class into thead for change color
					 * 
					 */

					/**
					 * when click to 「キャンセル」ボタン
					 */
					$(document).on("click", ".btn-cancel", function() {
						var histor = document.referrer;
						if (histor.indexOf('search.action') > -1)
							location.href = "search.action";
						else
							location.href = "history.action";
						return false;
					});
					$("#txt thead tr").addClass("success");
					/**
					 * when click to button []
					 */
					$(document).on("click", ".btn-register", function() {
						javascript: location.href = 'search.action';
						return false;
					});
					$(document).on("click", ".btn-history", function() {
						javascript: location.href = 'history.action';
						return false;
					});
					$(document).on("click", ".btn-edit", function() {
						javascript: location.href = 'yoyakulist.action';
						return false;
					});
					$(document).on("click", ".btn-passwdchange", function() {
						javascript: location.href = 'changepasswd.action';
						return false;
					});

					/**
					 * submit form when click to line
					 */
					$(document).on("click", ".bookingto", function() {
						var id = $(this).attr("id");
						$(".bookingform_" + id).submit();
						return false;
					});

					/**
					 * 重量計算 ： （取得した容量に対する重量/1000）*入力された数量
					 * 「タブ」キーあるいは重量テキストボックスにフォーカスすると、自動に計算する。
					 */

					/**
					 * using ajax to send data when user choise 「容量」select box
					 * 
					 */
					$(document).on(
							"change",
							".capacity-choise",
							function() {
								resetDom();
								var defaultval = $(this).val();
								if (defaultval == -1)
									setCapacityWhenloadPage();
								else {
									var data = {};
									data.productcapacity = $(this).val();
									data.productweight = $(this).find(
											'option:selected').attr('weight');
									var url = 'callAjaxType.action';
									var type = 'POST';
									var async = false;
									var dataType = 'json';
									$(".load-ajax-ef").fadeIn("nomal");
									sendData(url, type, dataType, async, data);
									resetValueInput();
									$(".load-ajax-ef").fadeOut("nomal");
								}
							});
					/**
					 * using ajax to send data to server when user choose
					 * 「チェック」button
					 * 
					 */
					$(document)
							.on(
									"keydown",
									".input_check",
									function(e) {
										var readonly = $(this).attr("readonly");
										if (e.keyCode == 9
												&& readonly != "readonly") {
											var check = true;
											var data = {};
											data.newquantity = $(
													".input_quantity").val();
											data.productweight = $(
													".capacity-choise").find(
													'option:selected').attr(
													'weight');
											data.newweight = $(".input_weight")
													.val();
											if (typeof (define.type) === 'undefined'
													|| define.type == "")
												data.type = $(".hd_type").val();
											else
												data.type = define.type;
											if ($(this).attr("class").indexOf(
													"quantity") > -1) {
												var val = $(
														"input[name='productquantity']")
														.val();
												if (typeof (val) === 'undefined')
													val = $("input[name='qty']")
															.val();
												check = validateNumber(val);
												data.actionType = define.CHECK_TYPE_QUANTITY;
											} else if ($(this).attr("class")
													.indexOf("weight") > -1) {
												var val = $(
														"input[name='productweight']")
														.val();
												if (typeof (val) === 'undefined')
													val = $(
															"input[name='weight']")
															.val();
												check = validateNumber(val);
												data.actionType = define.CHECK_TYPE_WEIGHT;
											}
											data.seqno = $(".hd_seqno").val();
											if (check
													|| typeof (check) === 'undefined') {
												var url = 'callAjaxCalculation.action';
												var type = 'POST';
												var async = false;
												var dataType = 'json';
												$(".load-ajax-ef").fadeIn(
														"nomal");
												sendData(url, type, dataType,
														async, data);
												$(".load-ajax-ef").fadeOut(
														"nomal");
												// resetDom();
											} else {
												$(".input_check").val(0);
												$(this).focus();

											}
										}
									});

					/**
					 * send data using ajax
					 */
					function sendData(url, type, dataType, async, ajaxData) {
						var util = new Util();
						var ajax = util.ajax(type, url, dataType, async,
								ajaxData);
						ajax.done(function(resp) {
							define.type = resp.type;
							if (resp.type == define.TYPE_KAMIBUKURO) {
								setValueInput(".input_weight", resp.rtweight);
							} else if (resp.type == define.TYPE_BARA) {
								setValueInput(".input_quantity",
										resp.rtquantity);
								if (resp.nokisaki != -1) {
									if (resp.rtweight > resp.nokisaki) {
										var errors = [];
										errors.push(message.data.OVER_NOKISAKI
												+ "(" + resp.nokisaki + ")");
										reportError(errors)
										$(".form-jichiku").val(-1);
									}
								}
							} else if (resp.type == define.TYPE_TB) {
								setValueInput(".input_quantity",
										resp.rtquantity);
							}
							showDomByType(resp.type);
						});
						ajax.fail(function(error) {
							reportError(error);
						});
					}

					function setValueInput(object, value) {
						$(object).val(value);
					}
					function resetValueInput() {
						$(".input_quantity").val(0);
						$(".input_weight").val(0);
					}

					function calculateWeight(fixWeight, quantity) {
						var weight = 0;
						if (quantity == 0)
							weight = 0;
						else {
							weight = (fixWeight / 1000) * quantity;
						}
						return weight;
					}

					/**
					 * when load page: disable quantity and weight input box
					 */
					$(function() {
						var formcd = $(".capacity-choise").val();
						var type = define.TYPE_OTHER;
						if (typeof (formcd) !== 'undefined') {
							if (formcd == -1) {
								setCapacityWhenloadPage(formcd);
							} else if (formcd.indexOf('R') > -1
									&& formcd.indexOf('K') == -1)
								type = define.TYPE_BARA;
							else if (formcd.indexOf('K') > -1
									&& formcd.indexOf('R') == -1)
								type = define.TYPE_TB;
							else if ((parseFloat(formcd) == parseInt(formcd))
									&& !isNaN(formcd))
								type = define.TYPE_KAMIBUKURO;
							else
								type = define.TYPE_OTHER;
							showDomByType(type);
						}
					});

					/**
					 * when choise 「分場」セレクトボックス 詳細：
					 * 
					 */
					$(document)
							.on(
									"change",
									".form-jichiku",
									function() {
										// send data to server to check weight
										var check = true;
										var data = {};
										data.newweight = $(".input_weight")
												.val();
										data.jichikucd = $(this).val();
										if (data.jichikucd != -1) {// default
											// value
											data.type = define.type;
											if (data.type == define.TYPE_BARA) {
												var val = $(
														"input[name='productweight']")
														.val();
												if (typeof (val) === 'undefined')
													val = $(
															"input[name='weight']")
															.val();
												check = validateNumber(val);
												data.actionType = define.CHECK_TYPE_WEIGHT;
												if (check
														|| typeof (check) === 'undefined') {
													var url = 'callAjaxCalculation.action';
													var type = 'POST';
													var async = false;
													var dataType = 'json';
													$(".load-ajax-ef").fadeIn(
															"nomal");
													sendData(url, type,
															dataType, async,
															data);
													$(".load-ajax-ef").fadeOut(
															"nomal");
													// resetDom();
												}
											}
										}
									});

					/**
					 * add tool tip for input text elements [quantity] and
					 * [weight]
					 */
					$(function() {
						$(".input_check").qtip({
							prerender : true,
							content : message.data.caption,
							position : {
								my : 'top left',
								at : 'bottom right'
							}
						})
					});
					/**
					 * add tool tip for input text in change passwd interface
					 */
					$(function() {
						$(".form_changepwd input").qtip({
							prerender : true,
							content : message.data.caption_changepasswd,
							position : {
								my : 'top left',
								at : 'bottom right'
							}
						});
					});
					/**
					 * add qtip for 「ヘルプ」ボタン
					 * 
					 */
					$(function() {
						$(".help-icon").qtip({
							prerender : true,
							content : message.data.HELP,
							position : {
								my : 'top right',
								at : 'bottom left'
							}
						});
					});
					function setCapacityWhenloadPage() {
						$(".input_weight").val(0);
						$(".input_quantity").val(0);
						$(".input_weight").attr("readonly", "readonly");
						$(".weight-check").attr("disabled", "disabled");
						$(".input_quantity").attr("readonly", "readonly");
						$(".quantity-check").attr("disabled", "disabled");
					}
					function showDomByType(type) {
						if (type == define.TYPE_KAMIBUKURO) {
							$(".input_weight").attr("readonly", "readonly");
							$(".weight-check").attr("disabled", "disabled");
						}
						if (type == define.TYPE_TB) {
							$(".input_quantity").attr("readonly", "readonly");
							$(".quantity-check").attr("disabled", "disabled");
						}
						if (type == define.TYPE_BARA) {
							$(".input_quantity").attr("readonly", "readonly");
							$(".quantity-check").attr("disabled", "disabled");
						}
						if (type == define.TYPE_OTHER) {

						}
					}
					function resetDom() {
						$(".input_quantity").removeAttr("readonly");
						$(".quantity-check").removeAttr("disabled");
						$(".input_weight").removeAttr("readonly");
						$(".weight-check").removeAttr("disabled");
					}
					function changeColor(object) {
						$(".btn-custom").removeClass("btn-warning");
						$(object).addClass("btn-warning");
					}

				});
